// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/CertificateManageControllerApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/CertificateReq'
import BaseClass from '@api/base/model/CertificateDto'

const AddClass = ListClass
const EditClass = ListClass
const DelClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'dto']

const transAdd = ({ fileList, ...other }) => {
  const certificateFile = JSON.stringify(fileList)
  return { ...other, certificateFile }
}

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'certificateQuery', DTO)
    return [resp.comQuery.totalCount, resp.dto || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.fileList = JSON.parse(item.certificateFile || '[]')
    })
    return arr
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd,
  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'certificateSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEdit: transAdd,
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'certificateUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del(one, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ...one, status: 3 }, BASE)
    await CORE.grpc(TIP, CLIENT, 'certificateUpdate', DTO)
    return CORE.grpcOK(TIP)
  },
}
